package com.msmall.community.weather.action.html;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.tiles.AttributeContext;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.preparer.ViewPreparer;

import com.msmall.community.ApplicationManager;
import com.msmall.community.weather.model.ForeCast;
import com.msmall.community.weather.model.Weather;
import com.msmall.community.weather.service.WeatherManager;

public class WeatherPreparer implements ViewPreparer{
	public void execute(TilesRequestContext tilesContext, AttributeContext context) {
		try {
			HttpServletRequest request = (HttpServletRequest) tilesContext.getRequest();
			HttpSession session = request.getSession();
			String city = null;
			try{
				city = (String) context.getAttribute("city").getValue();
			}catch(Exception e){
				
			}
			// 先读取页面传递过来的城市信息；
			// 如果没有从用户表userInfo读取设置的默认城市； 
			// 如果没有则是用默认;所以的地区信息默认使用行政区划码
			/**
			OnlineUser onlineUser = OnlineUser.getInstance(UserChecker.getUserkey(session));
			Long userid = onlineUser.getUserid();
			UserInfoManager usrManager = (UserInfoManager) ApplicationManager.appContext.getBean("userInfoManager");
			if(null == city || "".equals(city)){
				city = usrManager.getByUserid(userid).getWeatherCode();
				if(city == null || "".equals(city)){
					city = WeatherManager.DEFAULT_WEATHER_CODE;
				}
			}
			*/
			WeatherManager weatherManager = (WeatherManager) ApplicationManager.appContext.getBean("weatherManager");
			Weather weather = weatherManager.getWeather(city);
			ForeCast foreCast = null;
			if(weather.getForeCasts() != null && weather.getForeCasts().size() > 0){
				foreCast = weather.getForeCasts().get(0);
			}
			
			request.setAttribute("weather",weather);
			request.setAttribute("foreCast",foreCast);
		} catch (Exception e) {
		}
	}
}
